﻿#region Namespace imports

using System;

#endregion

namespace AdventureGame
{
    internal class Puzzles
    {
        public static Boolean PuzzleModeBegin(String puzzle)
        {
            Console.WriteLine("This is a puzzle");
            Console.WriteLine("Normal commands are inactive.");

            if (puzzle == "torch")
            {
                if (Torch())
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            if (puzzle == "sand")
            {
                if (Sand())
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            if (puzzle == "marsh")
            {
                if (Marsh())
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        private static Boolean Torch()
        {
            Boolean[] torch = new Boolean[5] {true, true, true, false, false};

            Console.WriteLine("With a ghasp of horror you realize that there are no entrances or exits to this room!");
            


            while (true)
            {
                if (torch[0])
                {
                    Console.WriteLine("Torch one is lit");
                }
                else
                {
                    Console.WriteLine("Torch one is out");
                }
                if (torch[1])
                {
                    Console.WriteLine("Torch two is lit");
                }
                else
                {
                    Console.WriteLine("Torch two is out");
                }
                if (torch[2])
                {
                    Console.WriteLine("Torch three is lit");
                }
                else
                {
                    Console.WriteLine("Torch three is out");
                }
                if (torch[3])
                {
                    Console.WriteLine("Torch four is lit");
                }
                else
                {
                    Console.WriteLine("Torch four is out");
                }
                if (torch[4])
                {
                    Console.WriteLine("Torch five is lit");
                }
                else
                {
                    Console.WriteLine("Torch five is out");
                }
                Console.WriteLine();

                if (torch[0] && torch[2] && torch[4] && !torch[1] && !torch[3])
                {
                    Console.WriteLine(
                        "A whispering voice calls out \"Well done, the Pharoh awaits you to the North...\"");
                    Console.WriteLine("A large gate opens to the North!");
                    return true;
                }
                else
                {
                    Console.WriteLine();
                    Console.WriteLine("Press L to light and P to put out.");
                    String command = Console.ReadLine().Trim();
                    command = command.ToLower();

                    if (command == "l" || command == "light")
                    {
                        Console.WriteLine("Which torch will you light? (1, 2, 3, 4, or 5)");
                        command = Console.ReadLine().Trim();
                        command = command.ToLower();
                        Int32 torches = 0;

                        if (Int32.TryParse(command, out torches))
                        {
                            torches = Int32.Parse(command);
                            torch[torches - 1] = true;
                            Console.WriteLine("Torch {0} is lit", torches);
                        }
                        else
                        {
                            Console.WriteLine("Please type a number when selecting torches");
                        }
                    }
                    else if (command == "p" || command == "put out")
                    {
                        Console.WriteLine("Which torch will you put out? (1, 2, 3, 4, or 5)");
                        command = Console.ReadLine().Trim();
                        command = command.ToLower();
                        Int32 torches = 0;

                        if (Int32.TryParse(command, out torches))
                        {
                            torches = Int32.Parse(command);
                            torch[torches - 1] = false;
                            Console.WriteLine("Torch {0} is put out", torches);
                        }
                        else
                        {
                            Console.WriteLine("Please type a number when selecting torches");
                        }
                    }
                    else if (command == "e" || command == "exit" || command == "stop")
                    {
                        Console.WriteLine("You turn away from this conundrum in discust.");
                        return false;
                    }

                    if (torch[0] && torch[2] && torch[4] && !torch[1] && !torch[3])
                    {
                        Console.WriteLine(
                            "A whispering voice calls out \"Well done, the Pharoh awaits you to the North...\"");

                        return true;
                    }

                    Console.WriteLine();
                    Console.WriteLine("A whispering voice cackles: \"The torches are unaligned! Feel your punishment!\"");
                    Console.WriteLine();
                    String Battle = Combat.Battle("zombie");
                    if (Battle == "death")
                    {
                        return false;
                    }
                    else if (Battle == "flee")
                    {
                        Console.WriteLine("You dare flee from Death! There is no escape, mortal!");
                        continue;
                    }
                }
            }
        }

        private static Boolean Sand()
        {
            Console.WriteLine("As you enter this room, the door shuts behind you!");
            Console.WriteLine(
                "A cackling voice cries out \"Young and foolish adventurer! You have dared encroach upon the Pharoh's grounds! You have but one chance to redeem yourself... I have three orbs. Each is one color. Those colors could be red, black, green, or white. Several orbs may share a color. Tell me the colors of my orbs... before time runs out!\"");
            Console.WriteLine();
            Console.WriteLine("The room begins filling with sand!");
            Console.WriteLine();

            char[] aColors = {'g', 'w', 'r', 'b'};
            char[] orbs = {'g', 'w', 'g'};

            Random rando = new Random();
            Random r = new Random();
            Random o = new Random();

            orbs[0] = aColors[rando.Next(4)];
            orbs[1] = aColors[rando.Next(4)];
            orbs[2] = aColors[rando.Next(4)];

            Boolean color1 = false;
            Boolean color2 = false;
            Boolean color3 = false;

            Int32 SandLevel = 0;

            String[] sands = {
                                 "The sand is up to your ankles", "The sand is up to your knees",
                                 "The sand is up to your waist", "The sand is up to your shoulders",
                                 "The sand is up to your neck", "The sand flows over your face..."
                             };

            for (; SandLevel < 5; SandLevel++)
            {
                if (color1 && color2 && color3)
                {
                    Console.WriteLine("\"Well done\"");
                    Console.WriteLine("\"Your next trial awaits east of the Grand Buriel Chamber!\"");
                    World._world2["grand buriel chamber"]._east = "secret passage";
                    return true;
                }

                color1 = false;
                color2 = false;
                color3 = false;


                Console.WriteLine();
                Console.WriteLine(sands[SandLevel]);
                Console.WriteLine("What is your answer? (example: \"rub\" or \"gbw\")");

                String command = Console.ReadLine().Trim();
                command = command.ToLower();
                char[] aCommand = command.ToCharArray();


                if (aCommand.Length == 3)
                {
                    Int32 itterations = 0;

                    foreach (char color in aCommand)
                    {
                        itterations++;

                        if (itterations == 1)
                        {
                            if (color == orbs[itterations - 1])
                            {
                                Console.WriteLine("Color {0} is correct", itterations);
                                color1 = true;
                            }
                            else
                            {
                                Console.WriteLine("Color {0} is incorrect", itterations);
                            }
                        }
                        else if (itterations == 2)
                        {
                            if (color == orbs[itterations - 1])
                            {
                                Console.WriteLine("Color {0} is correct", itterations);
                                color2 = true;
                            }
                            else
                            {
                                Console.WriteLine("Color {0} is incorrect", itterations);
                            }
                        }
                        else if (itterations == 3)
                        {
                            if (color == orbs[itterations - 1])
                            {
                                Console.WriteLine("Color {0} is correct", itterations);
                                color3 = true;
                            }
                            else
                            {
                                Console.WriteLine("Color {0} is incorrect", itterations);
                            }
                        }
                    }
                }
                else
                {
                    Console.WriteLine("That's not three colors each represented by a single letter. Example: bub or uur");
                }
            }


            return false;
        }


        private static Boolean Marsh()
        {
            String[] quicksand = {
                                     "You escape!", "The quicksand is up to your knees",
                                     "The quicksand is up to your waist", "The quicksand is up to your shoulders",
                                     "The quicksand is up to your neck", "The quicksand is up to your mouth",
                                     "The quicksand flows over your head..."
                                 };
            Int32 i = 2;

            for (i = 2; i < quicksand.Length - 1;)
            {
                Console.WriteLine();
                Random rand = new Random();
                Console.WriteLine(quicksand[i]);

                if (i <= 0)
                {
                    return true;
                }


                Console.WriteLine("Press S to struggle, R to relax, or G to try to grab something.");
                String command = Console.ReadLine().Trim();
                command = command.ToLower();

                if (command == "s")
                {
                    Console.WriteLine("You struggle!");
                    Int32 chance = rand.Next(10);
                    if (chance >= 5)
                    {
                        Int32 escape = rand.Next(4);
                        i = i - escape;
                        if (i < 0)
                        {
                            i = 0;
                        }
                        Console.WriteLine("You pull free a bit!");
                        continue;
                    }
                    else
                    {
                        Console.WriteLine("Your struggles pull you deeper");
                        Int32 escape = rand.Next(2);
                        i = i + escape;
                        if (i > quicksand.Length - 1)
                        {
                            i = quicksand.Length - 1;
                        }
                    }
                }
                else if (command == "r")
                {
                    Console.WriteLine("You relax");
                    Int32 chance = rand.Next(10);

                    if (chance >= 4)
                    {
                        Int32 escape = rand.Next(3);
                        i = i - escape;
                        if (i < 0)
                        {
                            i = 0;
                        }
                        Console.WriteLine("you feel yourself pulling free!");
                        continue;
                    }
                    else
                    {
                        Console.WriteLine("You're still being pulled in!");
                        i++;
                        if (i > quicksand.Length - 1)
                        {
                            i = quicksand.Length - 1;
                        }
                    }
                }
                else if (command == "g")
                {
                    Console.WriteLine("You grasp for a handhold!");
                    Int32 chance = rand.Next(10);
                    chance = chance - i;

                    if (chance >= 6)
                    {
                        Console.WriteLine("You grab onto a branch!");
                        i = 0;
                        continue;
                    }
                    else
                    {
                        Console.WriteLine("Your attempt was in vain!");
                        Int32 escape = rand.Next(4);
                        i = i + escape;
                        if (i > quicksand.Length - 1)
                        {
                            i = quicksand.Length - 1;
                        }
                    }
                }
                else
                {
                    Console.WriteLine("your command was incomprehensible");
                }
            }
            Console.WriteLine(quicksand[i]);
            Console.WriteLine("You die...");
            return false;
        }

        public static String Lake()
        {
            Console.WriteLine(
                "You step out onto the ice and everything seems fine... Until, halfway across, the ice around you, especially to the East begins to crack!");
            String[] lake = {
                                "Western shore", "Western half of the lake", "Hard ice", "Firmer ice", "Center of the lake"
                                , "Fragile ice", "Brittle ice", "Eastern half of the lake", "Eastern shore"
                            };
            Int32 side = 4;
            decimal stress = 10;
            Console.WriteLine();
            while (true)
            {
                Console.WriteLine();

                if (side <= 0 || side >= lake.Length - 1)
                {
                    String shore = "ERROR";
                    if (side <= 0)
                    {
                        shore = "western";
                    }
                    else if (side >= lake.Length - 1)
                    {
                        shore = "eastern";
                    }
                    Console.WriteLine("You make it to the {0} shore", shore);
                    if (side <= 0)
                    {
                        Console.WriteLine("You gained {0} Xp!", 30);
                        Hero.SetXp(30);
                        if (Hero.GrabXp() >= Hero.GrabToNextLevel())
                        {
                            Hero.LevelUp();
                        }

                        return "w";
                    }
                    else if (side >= lake.Length - 1)
                    {
                        Console.WriteLine("You gained {0} Xp!", 100);
                        Hero.SetXp(100);
                        if (Hero.GrabXp() >= Hero.GrabToNextLevel())
                        {
                            Hero.LevelUp();
                        }

                        return "e";
                    }
                }

                if (stress <= 0)
                {
                    Console.WriteLine("The ice all around you collapses!!");
                    Console.WriteLine("You are pulled under the freezing cold water!");
                    Console.WriteLine("You die...");
                    return "death";
                }

                Console.WriteLine("You are on the {0}", lake[side]);
                Console.WriteLine("Ice integrity is at {0}%", stress*10);
                Console.WriteLine();
                Console.WriteLine("Do you travel East or West?");
                String command = Console.ReadLine().Trim();
                command = command.ToLower();

                if (command == "w" || command == "west")
                {
                    Console.WriteLine("Do you run, walk or jump? (r, w, or j)");
                    String command2 = Console.ReadLine().Trim();
                    command2 = command2.ToLower();
                    Random rand = new Random();

                    if (command2 == "r")
                    {
                        Console.WriteLine("You run west with the ice cracking in front of you!");
                        side = side - 2;
                        decimal newstress = rand.Next((side + 2) - (Hero.GrabEva()/30));
                        if (newstress <= 1)
                        {
                            newstress = 1;
                        }
                        stress = stress - newstress;
                        continue;
                    }
                    else if (command2 == "w")
                    {
                        Console.WriteLine("You walk west and the ice cracks a little.");
                        side--;
                        decimal newstress = rand.Next((side) - (Hero.GrabEva()/30));
                        if (newstress <= 0)
                        {
                            newstress = 0;
                        }
                        stress = stress - newstress;
                        continue;
                    }
                    else if (command2 == "j")
                    {
                        Console.WriteLine("You make a bold leap to the west!");
                        if (rand.Next(Hero.GrabEva()) >= 18)
                        {
                            Console.WriteLine("You land badly and the ice shatters!");
                            decimal newstress = rand.Next((side + 3) - (Hero.GrabEva()/30));
                            if (newstress <= 1)
                            {
                                newstress = 1;
                            }
                            stress = stress - newstress;
                        }
                        else
                        {
                            Console.WriteLine("You land perfectly without damaging the ice!");
                            side--;
                        }

                        continue;
                    }
                    else
                    {
                        Console.WriteLine("Please use r, w, or j");
                        continue;
                    }
                }
                else if (command == "e" || command == "east")
                {
                    Console.WriteLine("Do you run, walk or jump? (r, w, or j)");
                    String command2 = Console.ReadLine().Trim();
                    command2 = command2.ToLower();
                    Random rand = new Random();

                    if (command2 == "r")
                    {
                        Console.WriteLine("You run east with the ice cracking behind you!");
                        side = side + 2;
                        decimal newstress = rand.Next((side + 2) - (Hero.GrabEva()/30));
                        if (newstress <= 1)
                        {
                            newstress = 1;
                        }
                        stress = stress - newstress;
                        continue;
                    }
                    else if (command2 == "w")
                    {
                        Console.WriteLine("You walk east and the ice cracks a little.");
                        side++;
                        decimal newstress = rand.Next((side) - (Hero.GrabEva()/30));
                        if (newstress <= 0)
                        {
                            newstress = 0;
                        }
                        stress = stress - newstress;
                        continue;
                    }
                    else if (command2 == "j")
                    {
                        Console.WriteLine("You make a bold leap to the east!");
                        if (Hero.GrabEva() >= 15)
                        {
                            Console.WriteLine("You land badly and the ice shatters!");
                            decimal newstress = rand.Next((side + 3) - (Hero.GrabEva()/30));
                            if (newstress <= 1)
                            {
                                newstress = 1;
                            }
                            stress = stress - newstress;
                        }
                        else
                        {
                            Console.WriteLine("You land perfectly without damaging the ice!");
                            side++;
                        }

                        continue;
                    }
                    else
                    {
                        Console.WriteLine("Please use r, w, or j");
                        continue;
                    }
                }
                else
                {
                    Console.WriteLine("Your command is incomprehensible");
                    continue;
                }
            }
        }


        public static String Waterfall()
        {
            Console.WriteLine("You steel yourself and begin to climb...");
            String[] falls = {
                                 "Bottom", "lower portion", "lower ledge", "middle", "higher ledge", "upper portion", "top"
                                 ,
                             };
            Int32 part = 1;

            while (true)
            {
                if (Hero.GrabHp() <= 0)
                {
                    Console.WriteLine("You feel the last of your strength leave you. Your grip falters and you fall!");
                    Console.WriteLine("You die...");
                    return "death";
                }
                if (part <= 0)
                {
                    Console.WriteLine("You arrive at the bottom of the falls in defeat");
                    return "w";
                }
                if (part >= falls.Length - 1)
                {
                    Console.WriteLine("You climb exhausted but triumphant to the top of the frozen falls");

                    Console.WriteLine("You gained {0} Xp!", 100);
                    Hero.SetXp(100);
                    if (Hero.GrabXp() >= Hero.GrabToNextLevel())
                    {
                        Hero.LevelUp();
                    }
                    return "e";
                }

                Console.WriteLine();
                Console.WriteLine("Hp: {0}", Hero.GrabHp());
                Console.WriteLine("You are at the {0} of the waterfall", falls[part]);
                Console.WriteLine("Do you climb up or lower yourself down? (U/D)");

                String command = Console.ReadLine().Trim();
                command = command.ToLower();
                Console.WriteLine();

                if (command == "u")
                {
                    Console.WriteLine("You climb ever upward...");
                    part++;

                    Random randu = new Random();
                    if (randu.Next(10) - (Hero.GrabEva()/10) >= 2)
                    {
                        Console.WriteLine("You slip and cut yourself!");
                        Int32 damage = randu.Next(10) - Hero.GrabDef();
                        if (damage <= 0)
                        {
                            damage = 1;
                        }
                        Hero.SetHp(damage);
                        Console.WriteLine("You take {0} damage!", damage);
                    }
                }
                else if (command == "d")
                {
                    Console.WriteLine("You lower yourself downward");
                    part--;

                    Random randd = new Random();
                    if (randd.Next(10) - (Hero.GrabEva()/10) >= 3)
                    {
                        Console.WriteLine("You slip and cut yourself!");
                        Int32 damage = randd.Next(10) - Hero.GrabDef();
                        if (damage <= 0)
                        {
                            damage = 1;
                        }
                        Hero.SetHp(damage);
                        Console.WriteLine("You take {0} damage!", damage);
                    }
                    Console.WriteLine("You lower yourself downward");
                    part--;
                }
                else
                {
                    Console.WriteLine("Your command is incomprehensible");
                    continue;
                }

                Random rand = new Random();

                if (rand.Next(10) - (Hero.GrabEva()/10) >= 2)
                {
                    Console.WriteLine("Your grip slips and you fall downward!");
                    Console.WriteLine("Do you grab on to a sharp protrusion or fall lower onto a safer ledge? (G/F)");
                    command = Console.ReadLine().Trim();
                    command = command.ToLower();
                    Console.WriteLine();

                    if (command == "g")
                    {
                        Console.WriteLine("You boldly grab a sharp piece of ice!");


                        Int32 damage = rand.Next(20) - Hero.GrabDef();
                        if (damage <= 0)
                        {
                            damage = 1;
                        }
                        Hero.SetHp(damage);
                        Console.WriteLine("You take {0} damage!", damage);
                    }

                    if (command == "f")
                    {
                        Console.WriteLine("You fall a short ways.");
                        part--;

                        if (rand.Next(10) - (Hero.GrabEva()/10) >= 3)
                        {
                            Console.WriteLine("As you fall, you are cut and bruised!");
                            Int32 damage = rand.Next(10) - Hero.GrabDef();
                            if (damage <= 0)
                            {
                                damage = 1;
                            }
                            Hero.SetHp(damage);
                            Console.WriteLine("You take {0} damage!", damage);
                        }
                    }
                }
            }
        }
    }
}